// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.

package fat
{

import com.fit4as.fit.*;
import mx.utils.StringUtil;
import com.fit4as.util.reflect.Field;


public class Equals extends PrimitiveFixture {

    private var heads : Parse;
    private var type : TypeAdapter;
    private var x : Object;
    private var y : Object;

    override public function doRows( rows : Parse ) : void 
    {
        heads = rows.parts;
        super.doRows(rows.more);
    }

    override public function doCell( cell : Parse, col : int ) : void 
    {
        try {
            var head : String = heads.at( col ).text().charAt(0);
            //t race("Equals " + [head, cell.text(), type, type!=null?type.type : null]);
            switch (head) {
                case 't':   type = getType( cell.text() ); break;
                case 'x':   x = type.parse( cell.text() ); break;
                case 'y':   y = type.parse( cell.text() ); break;
                case '=':   check( cell, type.equals( x, y )); break;
                case '?':   cell.addToBody( info("x: " + print( x ) + " y: " + print( y ))); break;

                default:    throw new Exception("don't do " + head);
            }
        } catch( e : Exception ){
            exception( cell, e );
        }
    }

    static private var IntegerArray : Array = [];
    static private var BooleanArray : Array = [];
    static private var StringArray : Array = [];

    private function getType( name : String ) : TypeAdapter {
        var type : Class = null;
        var componentType : Class = null;
        switch( name )
        { 
        	case "boolean" : type = Boolean; break;
            case "integer" : int; break;
            case "real" : type = Number; break;
            case "string" : type = String; break;
            case "date" : type = Date; break;
            case "money" : type = Money; break;
            case "scientific" : type = ScientificDouble; break;
            case "integers" : type = Array; componentType = int; break;
            case "booleans" : type = Array; componentType = Boolean; break;
            case "strings" : type = Array; componentType = String; break;
        }
        var field : Field = new Field( Equals, name, type, componentType ); 
        return TypeAdapter.onField( this, field );
    }

    override public function parse( s : String, type : Class ) : Object 
    {
        //t race("Equals#parse " + [ s, type ] );
        if( type == Money ) return new Money( s );
        if( type == Boolean ) return parseCustomBoolean( s );
        if( type == Array ) return s.split(",");
        return super.parse( s, type );
    }

    private function parseCustomBoolean( s : String ) : Boolean 
    {
        if (true) throw new Exception("boolean");
        
        var firstChar : String = s.charAt(0);
        switch( firstChar )
        {
        	case "y" : 
        	case "t" : return true;
        	case "n" :
        	case "f" : return false;
        }
        
        return null;	
    }

    private function print(value : Object) : String {
        return type.toString(value);
    }

}

}